package cn.edu.lit.jsjxy.CourseExample.chapter4;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import cn.edu.lit.jsjxy.CourseExample.R;

public class DrawingActivity_4_5 extends AppCompatActivity implements View
        .OnClickListener, View.OnTouchListener
{

    int startX, startY, endx, endy;
    private HandWrite handWriteView = null;
    private Button btnClear, btnRestore;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawing_activity4_5);

        handWriteView = (HandWrite) findViewById(R.id.handwriteview);

        btnClear = (Button) findViewById(R.id.btnClear);
        btnClear.setOnClickListener(this);

        btnRestore = (Button) findViewById(R.id.btnRestore);
        btnRestore.setOnClickListener(this);

        handWriteView.setOnTouchListener(this);
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId()) {
            case R.id.btnClear:
                handWriteView.clear();
                break;
            case R.id.btnRestore:
                handWriteView.restore();
                break;
        }
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent)
    {

        //        clickX = (int)handWriteView.getX();
        //        clickY = (int)handWriteView.getY();

        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = (int) motionEvent.getX();
                startY = (int) motionEvent.getY();
                handWriteView.drawPoint(startX, startY);
                break;
            case MotionEvent.ACTION_UP:
                //                endx = (int) motionEvent.getX();
                //                endy = (int) motionEvent.getY();
                //                handWriteView.drawLine(clickX, clickY, endx, endy);
                //                clickX = endx;
                //                clickY = endy;

                break;

            case MotionEvent.ACTION_MOVE:
                endx = (int) motionEvent.getX();
                endy = (int) motionEvent.getY();
                handWriteView.drawLine(startX, startY, endx, endy);
                startX = endx;
                startY = endy;
                break;
        }

        return true;
    }
}
